package cn.xsw.controller;


import cn.xsw.common.ResultBody;
import cn.xsw.entity.Carousel;
import cn.xsw.service.CarouselService;
import cn.xsw.utils.BaseQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangYiHui
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/carousel")
@Api(tags = "轮播图接口")
public class CarouselController extends BaseController{

    @Resource
    private CarouselService carouselService;


    @GetMapping("/findAll")
    public ResultBody findAll(){
        return ResultBody.success(carouselService.list(new QueryWrapper<Carousel>().orderBy(true,true,"sort_key")));
    }

    @GetMapping("/findPage")
    public ResultBody findPage(BaseQuery<Carousel> query){
            Page page = carouselService.page(query.page());
            return ResultBody.success().page(page);
    }


    @GetMapping("/delById/{id}")
    public ResultBody delById(@PathVariable("id") int id){
            Boolean b = carouselService.removeById(id);
            if(!b) return ResultBody.error("删除失败");
            return ResultBody.success();
    }

    @PostMapping("/add")
    public ResultBody add(@RequestBody Carousel carousel){
            Boolean b = carouselService.save(carousel);
            if(!b) return ResultBody.error("添加失败");
            return ResultBody.success();
    }
    @PostMapping("/updateById")
    public ResultBody updateById(@RequestBody Carousel carousel){
            boolean b = carouselService.updateById(carousel);
            if(!b) return ResultBody.error("更新失败");
            return ResultBody.success();
    }

}

